<?php
	require_once("../Datos/Conexion.php");
	if(!isset($_SESSION)){
		session_start();
	}
	
	class Login{
	
		function Login($usuario='', $password='', $operacion='ver'){
			
			if($usuario == "" || $password == ""){
				if(@$_SESSION['login']['log_id'] == NULL || @$_SESSION['login']['log_usuario'] == NULL || 
				@$_SESSION['login']['log_nombre'] == NULL || @$_SESSION['login']['log_apellido'] == NULL || 
				@$_SESSION['login']['log_nivel_acceso'] == NULL){

					header('Location: Index.php');
				}
			}
			else{
				$conexion = new Conexion();
				$consultas[0] = sprintf("SELECT u_id, u_nombre, u_apellido, u_nivel_acceso FROM 
									ec_usuarios	WHERE u_login = '%s' AND u_password = '%s'",
									$usuario, $password);
				
				$rdo = $conexion->Ejecutar($consultas);
				
				$i=0;
				while($row_rdo = mysql_fetch_assoc($rdo[0])){
					$arr_rdo[$i] = $row_rdo;
					$i++;
				}
				
				if(sizeof($arr_rdo > 0)){
					$_SESSION['login']['log_usuario'] = $usuario;
					$_SESSION['login']['log_id'] = $arr_rdo[0]['u_id'];
					$_SESSION['login']['log_nombre'] = $arr_rdo[0]['u_nombre'];
					$_SESSION['login']['log_apellido'] = $arr_rdo[0]['u_apellido'];
					$_SESSION['login']['log_nivel_acceso'] = $arr_rdo[0]['u_nivel_acceso'];
				}
			}
		}	
		
		function cerrarSesion(){
			unset($_SESSION['login']);/*
			$_SESSION['login']['log_usuario'] = NULL;
			$_SESSION['login']['log_id'] = NULL;
			$_SESSION['login']['log_nombre'] = NULL;
			$_SESSION['login']['log_apellido'] = NULL;
			$_SESSION['login']['log_nivel_acceso'] = NULL;*/
			header('Location: Index.php');
		}
		
		function Autorizar($operacion = 'ver'){
			$nombre = strtolower(strrev(substr(strrev($_SERVER['PHP_SELF']), 0, strpos(strrev($_SERVER['PHP_SELF']), '/'))));
			
			$dom = new DomDocument();
			$dom->load('xsl/navegacion.xml');
			
			$enlaces = $dom->getElementsByTagName("enlace");
			foreach($enlaces as $enlace){
				if(strtolower($enlace->getAttribute("enlace")) == $nombre){
					if($enlace->getElementsByTagName($operacion)->item(0)->getAttribute("nivel") <= $_SESSION['login']['log_nivel_acceso']){
						return true;
					}
					else{
						return false;
					}
				}
			}
			
		}
	
	}
?>